package com.ybxx.hr.action;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.util.DateUtil;
import com.ybxx.core.util.ExportExcel;
import com.ybxx.core.util.UUIDGenerator;
import com.ybxx.core.web.action.BaseAction;
import com.ybxx.hr.model.Dept;
import com.ybxx.hr.model.HrUser;
import com.ybxx.hr.model.School;
import com.ybxx.hr.model.Work;
import com.ybxx.hr.model.WorkContent;
import com.ybxx.hr.model.WorkType;
import com.ybxx.hr.model.vo.HrUserVO;
import com.ybxx.hr.service.DeptService;
import com.ybxx.hr.service.HrUserService;
import com.ybxx.hr.service.SchoolService;
import com.ybxx.hr.service.WorkContentService;
import com.ybxx.hr.service.WorkService;
import com.ybxx.hr.service.WorkTypeService;
import com.ybxx.system.SystemConstants;
import com.ybxx.system.model.User;

public class HrUserAction extends BaseAction {
	@Resource
	private DeptService deptService;
	@Resource
	private HrUserService hrUserService;
	@Resource
	private SchoolService schoolService;
	@Resource
	private WorkContentService workContentService;
	@Resource
	private WorkService workService;
	@Resource
	private WorkTypeService workTypeService;
	private List<Dept> depts;
	private List<HrUser> hrUsers;
	private List<School> schools;
	private List<WorkContent> workContents;
	private List<Work> works;
	private List<WorkType> workTypes;
	private List<HrUserVO> vos;
	private HrUser hrUser;
	private String msg="succ";
	/*打印*/
	public String print(){
		hrUsers=hrUserService.queryConditionExportExcel(hrUser);
		vos=new ArrayList<HrUserVO>();
		if(hrUsers!=null){
			int i=1;
			for(HrUser user:hrUsers){
				HrUserVO vo=new HrUserVO();
				vo.setSeq(i+"");
				vo.setAddr(user.getAddr());
				vo.setBirthday(user.getBirthday());
				vo.setContent(user.getContent());
				vo.setCtime(user.getCtime());
				vo.setDname(user.getDept().getDname());
				vo.setIcard(user.getIcard());
				vo.setLtime(user.getLtime());
				vo.setName(user.getName());
				vo.setSchool(user.getSchool().getSname());
				vo.setSex(user.getSex());
				vo.setState(user.getState());
				vo.setTel(user.getTel());
				vo.setWork(user.getWork().getWname());
				vo.setWorkContent(user.getWorkContent().getWcname());
				vo.setWorkmoney(user.getWorkmoney());
				vo.setWorkType(user.getWorkType().getWtname());
				vos.add(vo);
				i++;
			}
		}
		return "print";
	}
	/*导出excel*/
	public String conditionExportExcel(){
		hrUsers=hrUserService.queryConditionExportExcel(hrUser);
		List<HrUserVO> vos=new ArrayList<HrUserVO>();
		if(hrUsers!=null){
			int i=1;
			for(HrUser user:hrUsers){
				HrUserVO vo=new HrUserVO();
				vo.setSeq(i+"");
				vo.setAddr(user.getAddr());
				vo.setBirthday(user.getBirthday());
				vo.setContent(user.getContent());
				vo.setCtime(user.getCtime());
				vo.setDname(user.getDept().getDname());
				vo.setIcard(user.getIcard());
				vo.setLtime(user.getLtime());
				vo.setName(user.getName());
				vo.setSchool(user.getSchool().getSname());
				vo.setSex(user.getSex());
				vo.setState(user.getState());
				vo.setTel(user.getTel());
				vo.setWork(user.getWork().getWname());
				vo.setWorkContent(user.getWorkContent().getWcname());
				vo.setWorkmoney(user.getWorkmoney());
				vo.setWorkType(user.getWorkType().getWtname());
				vos.add(vo);
				i++;
			}
		}
		ExportExcel<HrUserVO> ex=new ExportExcel<HrUserVO>();
		OutputStream out;
		try {
			out = super.getResponse().getOutputStream();
			super.getResponse().reset();// 清空输出流
			super.getResponse().setContentType("text/html; charset=UTF-8");
			super.getResponse().setHeader("Content-disposition",
					"attachment; filename="+DateUtil.formatDateYYYYMMdd(new Date())+".xls");// 设定输出文件头
			super.getResponse().setContentType("application/msexcel");// 定义输出类型
			String[] headers={"序号","姓名","出生日期","性别","身份证号","岗位工资","参加工作时间","住址","电话","离职时间","状态","部门","学历","工种","岗位","用工方式","备注"};
	        ex.exportExcel("人事信息",headers, vos, out);
	        out.flush();
	        out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	/*删除*/
	public String remove(){
		try{
			hrUserService.remove(hrUser.getPuid());
		}catch(Exception ex){
			msg="用户删除失败";
		}finally{
			return "succ";
		}
	}
	/*查看*/
	public String view(){
		depts=deptService.getAll();
		schools=schoolService.getAll();
		workContents=workContentService.getAll();
		works=workService.getAll();
		workTypes=workTypeService.getAll();
		if(hrUser!=null && StringUtils.isNotEmpty(hrUser.getPuid())){
			hrUser=hrUserService.get(hrUser.getPuid());
		}
		return "view";
	}
	public String queryPage(){
		depts=deptService.getAll();
		workContents=workContentService.getAll();
		works=workService.getAll();
		/*查询人事信息*/
		QueryFilter filter=new QueryFilter(getRequest());
		hrUsers=hrUserService.queryPage(hrUser,filter);
		super.setRownum(filter.getPagingBean().getTotalItems());
		super.setCurrentpage(Integer.parseInt(filter.getPagingBean().getStart()+"")/Integer.parseInt(filter.getPagingBean().getPageSize()+"")+1);
		super.setPageSize(Integer.parseInt(filter.getPagingBean().getPageSize()+""));
		return "queryPage";
	}
	/*保存新增，修改信息*/
	public String save(){
		if(StringUtils.isEmpty(hrUser.getPuid())){
			hrUser.setPuid(UUIDGenerator.getUUID());
		}
		try{
			hrUserService.save(hrUser);
		}catch(Exception ex){
			msg="人事信息保存失败.";
		}finally{
			return "succ";
		}
	}
	/*加载修改，新增时候需要展示的数据*/
	public String load(){
		depts=deptService.getAll();
		schools=schoolService.getAll();
		workContents=workContentService.getAll();
		works=workService.getAll();
		workTypes=workTypeService.getAll();
		if(hrUser!=null && StringUtils.isNotEmpty(hrUser.getPuid())){
			hrUser=hrUserService.get(hrUser.getPuid());
		}
		return "load";
	}
	public List<Dept> getDepts() {
		return depts;
	}
	public void setDepts(List<Dept> depts) {
		this.depts = depts;
	}
	public List<HrUser> getHrUsers() {
		return hrUsers;
	}
	public void setHrUsers(List<HrUser> hrUsers) {
		this.hrUsers = hrUsers;
	}
	public List<School> getSchools() {
		return schools;
	}
	public void setSchools(List<School> schools) {
		this.schools = schools;
	}
	public List<WorkContent> getWorkContents() {
		return workContents;
	}
	public void setWorkContents(List<WorkContent> workContents) {
		this.workContents = workContents;
	}
	public List<Work> getWorks() {
		return works;
	}
	public void setWorks(List<Work> works) {
		this.works = works;
	}
	public List<WorkType> getWorkTypes() {
		return workTypes;
	}
	public void setWorkTypes(List<WorkType> workTypes) {
		this.workTypes = workTypes;
	}
	public HrUser getHrUser() {
		return hrUser;
	}
	public void setHrUser(HrUser hrUser) {
		this.hrUser = hrUser;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public List<HrUserVO> getVos() {
		return vos;
	}
	public void setVos(List<HrUserVO> vos) {
		this.vos = vos;
	}
	
	
}
